package cn.com.sd.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.com.sd.common.util.Page;
import cn.com.sd.system.dao.IPrivilegeDAO;
import cn.com.sd.system.model.Privilege;
import cn.com.sd.system.service.IPrivilegeService;

@Component(value = "privilegeService")
public class PrivilegeServiceImpl implements IPrivilegeService{

	@Resource(name = "privilegeDAO")
	private IPrivilegeDAO privilegeDAO;
	
	@Override
	public Integer getPrivilegeCount() {
		int count = privilegeDAO.getPrivilegeCount();
		if (count <= 0) {
			return 0;
		}
		return count;
	}

	
	@Override
	public List<Privilege> list(Page page) {
		return privilegeDAO.getPrivilegeList(page);
	}

	@Override
	public Privilege detail(Integer id) {
		Privilege privilege = privilegeDAO.getObject(id);
		if (privilege == null) {
			return null;
		}
		return privilege;
	}
	
	@Override
	public Boolean del(Integer id) {
		try {
			privilegeDAO.deleteObject(id);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Boolean delBanch(List<Integer> ids) {
		try {
			List<Privilege> privileges = new ArrayList<Privilege>();
			for (Integer id : ids) {
				if (id == null || id <= 0) {
					continue;
				}
				Privilege temp = new Privilege();
				temp.setId(id);
				privileges.add(temp);
			}
			privilegeDAO.deleteObjects(privileges);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Boolean saveOrUpdate(Privilege privilege) {
		try {
			privilegeDAO.saveObject(privilege);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}
}
